﻿Imports TMySQL
Public Class frmCreateTable
    Private _GridMyColumns As TGridMySQLColumn
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public Sub New(ByVal ASchemaName As String)
        Me.New()
        _GridMyColumns = New TGridMySQLColumn(GridCol)
        InitFrm()
        cboSchemaName.SelectedItem = ASchemaName
    End Sub
    Private ReadOnly Property DatabaseList() As TMyDatabaseList
        Get
            Return frmMain.DatabaseList
        End Get
    End Property
    Private ReadOnly Property Engines() As TMyEngines
        Get
            Return frmMain.Engines
        End Get
    End Property
    Private ReadOnly Property Collations() As TMyCollations
        Get
            Return frmMain.Collations
        End Get
    End Property
    Private Sub InitFrm()
        _GridMyColumns.InitColumns()
        Common.SetGridAllowSorting(GridCol, DataGridViewColumnSortMode.NotSortable)
        InitEngine()
        InitCollation()
        InitSchema()
    End Sub
    Private Sub InitEngine()
        cboEngine.Items.Clear()
        For i As Integer = 0 To Engines.Count - 1
            cboEngine.Items.Add(Engines(i).Engine)
            If Engines(i).Engine.Equals("InnoDB") Then
                cboEngine.SelectedIndex = i
            End If
        Next
    End Sub
    Private Sub InitCollation()
        cboCollation.Items.Clear()
        For i As Integer = 0 To Collations.Count - 1
            cboCollation.Items.Add(Collations(i).CollationName)
            If Collations(i).DefaultCollation Then
                cboCollation.SelectedIndex = i
            End If
        Next
    End Sub
    Private Sub InitSchema()
        cboSchemaName.Items.Clear()
        For i As Integer = 0 To DatabaseList.Count - 1
            cboSchemaName.Items.Add(DatabaseList.Database(i).DatabaseName)
        Next
    End Sub

    Private Sub ValidateEntries()
        If cboSchemaName.SelectedItem Is Nothing OrElse cboSchemaName.SelectedIndex < 0 Then
            Throw New Exception("スキーマを指定してください")
        End If
        If txtTableName.Text.Length <= 0 Then
            Throw New Exception("テーブル名を入力してください")
        End If
        If cboEngine.SelectedItem Is Nothing OrElse cboEngine.SelectedIndex < 0 Then
            Throw New Exception("エンジンを指定してください")
        End If
        If GridCol.RowCount <= 0 Then
            Throw New Exception("列を１つ以上設定してください")
        End If

    End Sub
    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Try
            ValidateEntries()
            _GridMyColumns.ValidateEntries()
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            Return
        End Try

        _GridMyColumns.SchemaName = cboSchemaName.SelectedItem
        _GridMyColumns.TableName = txtTableName.Text
        _GridMyColumns.Engine = cboEngine.SelectedItem
        _GridMyColumns.Comment = txtComment.Text
        _GridMyColumns.Collation = cboCollation.SelectedItem
        Cursor.Current = Cursors.WaitCursor
        Try
            Dim SQL As String = _GridMyColumns.CreateTableSQL
            If Not Confirm(SQL) Then
                Return
            End If
            _GridMyColumns.CreateTable()
            Call MsgBox("テーブル作成しました", MsgBoxStyle.Information)
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Catch ex As Exception
            Call MsgBox("エラーが発生しました。" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Function Confirm(ByVal ASQL As String) As Boolean
        Dim frmRet As New frmConfirmMsg(ASQL)
        Try
            Return frmRet.ShowDialog = Windows.Forms.DialogResult.OK
        Finally
            frmRet.Dispose()
        End Try
    End Function

    Private Sub cmdAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRow.Click
        GridCol.Rows.Add()
    End Sub

    Private Sub cmdDeleteRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteRow.Click
        If GridCol.SelectedCells.Count <= 0 Then
            Return
        End If
        Dim row As Integer = GridCol.SelectedCells(0).RowIndex
        GridCol.Rows.RemoveAt(row)
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class